using System;
using System.IO;
using SharpSTS.CardControl;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace SharpSTS.Tests
{
    /// <summary>
    ///This is a test class for FilePathValidatorTest and is intended
    ///to contain all FilePathValidatorTest Unit Tests
    ///</summary>
    [TestClass()]
    public class FilePathValidatorTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for valid paths
        ///</summary>
        [TestMethod()]
        public void ValidateValidPathsTest()
        {
            bool expected = true;

            Assert.AreEqual(expected, FilePathValidator.IsValid(@"test\example.jpg"));
            Assert.AreEqual(expected, FilePathValidator.IsValid(@"example.jpg"));
            Assert.AreEqual(expected, FilePathValidator.IsValid(@".\example.jpg"));
            Assert.AreEqual(expected, FilePathValidator.IsValid(@"example"));
        }

        /// <summary>
        ///A test for invalid paths
        ///</summary>
        [TestMethod()]
        public void ValidateInvalidPathsTest()
        {
            bool expected = false;

            Assert.AreEqual(expected, FilePathValidator.IsValid(@"c:\windows\test\example.jpg"));
            Assert.AreEqual(expected, FilePathValidator.IsValid(@"..\example.jpg"));
            Assert.AreEqual(expected, FilePathValidator.IsValid(@".\example.jpg$data"));
            Assert.AreEqual(expected, FilePathValidator.IsValid(@"\example.jpg"));
        }

        /// <summary>
        ///A test for invalid filenames
        ///</summary>
        [TestMethod()]
        [DeploymentItem("SharpSTS.Core.dll")]
        public void IsFileNameValidInvalidTest()
        {
            bool expected = false;

            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid(""));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid(" "));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid("."));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid(" ."));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid(". "));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid(" . "));

            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid("COM1"));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid("COM1.txt"));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid("CON"));

            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid("test.txt$data"));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid("test.txt:data"));

            Random random = new Random();
            string dubiousFilename = string.Format("{0}",
                Path.GetInvalidFileNameChars()[random.Next(0, Path.GetInvalidFileNameChars().Length - 1)]);
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid(dubiousFilename));
        }

        /// <summary>
        /// Testing valid filenames, including a weirdy one.
        /// </summary>
        [TestMethod()]
        [DeploymentItem("SharpSTS.Core.dll")]
        public void IsFileNameValidTest()
        {
            bool expected = true;

            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid("test"));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid("test."));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid("test.txt"));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsFileNameValid("com.txt"));
        }

        /// <summary>
        ///A test for invalid directories
        ///</summary>
        [TestMethod()]
        [DeploymentItem("SharpSTS.Core.dll")]
        public void IsDirectoryInvalidTest()
        {
            bool expected = false;

            Assert.AreEqual(expected, FilePathValidator_Accessor.IsDirectoryValid(@"..\"));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsDirectoryValid("../"));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsDirectoryValid(@"c:\windows"));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsDirectoryValid(@"\windows"));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsDirectoryValid(@".\..\"));

            Random random = new Random();
            string dubiousDirectory = string.Format("{0}",
                Path.GetInvalidFileNameChars()[random.Next(0, Path.GetInvalidPathChars().Length - 1)]);
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsDirectoryValid(dubiousDirectory));
        }

        ///<summary>
        ///A test for valid directories
        ///</summary>
        [TestMethod()]
        [DeploymentItem("SharpSTS.Core.dll")]
        public void IsDirectoryValidTest()
        {
            bool expected = true;
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsDirectoryValid(null));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsDirectoryValid(""));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsDirectoryValid(@".\"));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsDirectoryValid(@"test\"));
            Assert.AreEqual(expected, FilePathValidator_Accessor.IsDirectoryValid(@"test"));
        }
    }
}
